﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nvn.Core.MVP;
using Hutech.CRM.Library.Views;
using Hutech.CRM.Library.Models.BusinessAccessLayer;
using Hutech.CRM.Library.Models.BusinessAccessLayer.BusinessObjects;
using Hutech.CRM.Library.Models.Entities;
using nvn.Library.Patterns;
using Hutech.CRM.Library.Utilities;

namespace Hutech.CRM.Library.Presenters
{
    public class ListCustomerPresenter : BasePresenter<IListCustomerView>
    {
        #region First Description
        public ListCustomerPresenter(IListCustomerView view)
            : base(view)
        {
            this.view.FristLoadEventHandler += new EventHandler<EventArgs>(view_FristLoadEventHandler);
            //this.view.SelectedIndexChanged_EventArgs += new EventHandler<OneParameterEventAgrs<int>>(view_SelectedIndexChanged_EventArgs);
            this.view.AccountCustomer_ItemCommand += new EventHandler<ItemCommandArgs<AccountCustomer>>(view_AccountCustomer_ItemCommand);
            this.view.AccountCustomer_ItemDataBound += new EventHandler<ItemDataBoundArgs>(view_AccountCustomer_ItemDataBound);
            this.view.Search_click += new EventHandler<OneParameterEventAgrs<SearchEntity>>(view_Search_click);
        }

        void view_Search_click(object sender, OneParameterEventAgrs<SearchEntity> e)
        {
            int totalPages;
            string keySearch=string.Empty;
            string kindSearch=string.Empty;
            IAccountCustomerBAL itemBAL = new AccountCustomerBAL();

            keySearch=RepairTextSearch(e.myType.TextSearch, out kindSearch);
            
            List<AccountCustomer> Customers = RepairData(itemBAL.GetPageForSearch(
                kindSearch,
                keySearch,
                e.myType.Job,
                e.myType.Position,
                e.myType.Province,
                e.myType.Level,
                view.PageNumber, view.PageSize, out totalPages));

            view.TotalPages = totalPages;

            view.AccountCustomers = Customers;
        }
        void view_AccountCustomer_ItemDataBound(object sender, ItemDataBoundArgs e)
        {
            throw new NotImplementedException();
        }
        void view_AccountCustomer_ItemCommand(object sender, ItemCommandArgs<AccountCustomer> e)
        {
            Guid DataKeyValue = new Guid(e.parameterValue);

            IAccountCustomerBAL itemBAL = new AccountCustomerBAL();
            AccountCustomer item = new AccountCustomer();//itemBAL. GetOne(DataKeyValue);
            switch (e.parameterName)
            {
                case "EditCommand":
                    view.EditItemIndex = e.itemIndex.Value;
                    view.Refresh = true;
                    
                    break;

                case "ApplyCommand":
                    //item.CriterionName = e.myType.CriterionName;
                    //item.GroupLevelCustomerGuid = e.myType.GroupLevelCustomerGuid;
                    //item.GroupLevelCustomerName = e.myType.GroupLevelCustomerName;
                    //item.IsActive = e.myType.IsActive;
                    //item.LastUpdatedDate = DateTime.Now;

                    //if (itemBAL.Save(item))
                    //{
                    //    view.EditItemIndex = -1;
                    //    view.CriterionCustomers = itemBAL.GetAll();
                    //}
                    //else
                    //{
                    //}
                    view.EditItemIndex = -1;
                    view.Refresh = true;
                    break;

                //case "DeleteCommand":
                //    itemBAL.Delete(DataKeyValue);
                //    view.CriterionCustomers = itemBAL.GetAll();
                //    //chu y chua xoa cac then con
                //    break;

                //case "EnableCommand":
                //    item.IsActive = e.myType.IsActive;
                //    if (itemBAL.Save(item))
                //        view.CriterionCustomers = itemBAL.GetAll();
                //    break;

                case "CancelCommand":
                    view.EditItemIndex = -1;
                    view.Refresh = true;
                    //WebUtils.SetupRedirect(this, Request.RawUrl);
                    break;
            }
        }
        //void view_SelectedIndexChanged_EventArgs(object sender, OneParameterEventAgrs<int> e)
        //{
        //    view.AccountCustomers = LoadCustomers();
        //}
        #endregion

        #region All Event
        void view_FristLoadEventHandler(object sender, EventArgs e)
        {
            //view.HeaderPopup = "Create new";
            //view.ShowPopup = true;
            RefreshAllControl();
        }
        #endregion

        #region private method
        private string RepairTextSearch(string item, out string kindSearch)
        {
            kindSearch = string.Empty;

            //Tach chuoi
            string[] temp = item.Split(':');
            if (temp.Length>1)
            {
                item = temp[1];
                kindSearch = temp[0].Trim().ToUpper();
            }
            else item = temp[0];

            //convert tieng viet co dau thanh khong dau
            item = StringHelper.RemoveDiacritics(item);

            //bo khoang trang, covert chu hoa
            item = item.Trim().ToUpper();

            return item;
        }
        //private List<AccountCustomer> LoadCustomers()
        //{
        //    int totalPages;
        //    IAccountCustomerBAL itemBAL = new AccountCustomerBAL();

        //    List<AccountCustomer> Customers = RepairData(itemBAL.GetPage(
        //        view.PageNumber, 
        //        view.PageSize, 
        //        out totalPages));
            
        //    view.TotalPages = totalPages;
        //    return Customers;
        //}
        private List<AccountCustomer> RepairData(List<AccountCustomer> Customers)
        {
            ICriterionCustomerBAL CriterionCustomerBAL=new CriterionCustomerBAL();
            List<TowTypeParameters<Guid, string>> criterionCustomer = CriterionCustomerBAL.GetGroupLevelCustomers(ConstantVariable.VALUE_DEFAULT_STRING_DROPDOWNLIST);

            foreach (AccountCustomer item in Customers)
            {
                int length = criterionCustomer.Count;
                for (int i = 0; i < length; i++)
                {
                    if (item.Level == criterionCustomer[i].T)
                    {
                        item.LevelName = criterionCustomer[i].V;
                        break;
                    }
                }
            }

            return Customers;
        }
        void RefreshAllControl()
        {
            view.PapeNumbers = ConstantVariable.PaperNumbers(); //Reload Dropdownlist Pages Control
            view.Refresh = true;
        }
        #endregion
    }
}
